package model;

import java.util.Date;

public class ConnectHandlerLineModel {
    private String ip;
    private int port;
    private Date lastWriteTime;
    private Date lastReadTime;
    private Date handlerCreateTime;
    private byte clientType;

    public ConnectHandlerLineModel(String ip, int port, Date lastWriteTime, Date lastReadTime, Date handlerCreateTime, byte clientType) {
        this.ip = ip;
        this.port = port;
        this.lastWriteTime = lastWriteTime;
        this.lastReadTime = lastReadTime;
        this.handlerCreateTime = handlerCreateTime;
        this.clientType = clientType;
    }

    @Override
    public String toString() {
        return this.getTrimString(ip, 15)
                + this.getTrimString(String.valueOf(port), 5)
                + this.getTrimString(String.valueOf(clientType), 3)
                + this.getTrimString(String.valueOf(lastWriteTime.getTime()), 15)
                + this.getTrimString(String.valueOf(lastReadTime.getTime()), 15)
                + this.getTrimString(String.valueOf(handlerCreateTime.getTime()), 15)
                ;
    }

    /**
     * 向右边加空格
     *
     * @param message 消息
     * @param size    结果长度
     * @return
     */
    private String getTrimString(String message, int size) {
        if (message == null) {
            return null;
        }
        final int length = message.length();
        if (length < size) {
            for (int i = 0; i < size - length; i++) {
                message += " ";
            }
            return message;
        } else if (length > size)
            return message.substring(0, size);
        else
            return message;
    }

}
